package controllers;

import models.Ingredient;
import models.Product;
import models.Salad;
import models.ShoppingCart;
import models.ShoppingCartLine;
import models.VeggieSalad;

public class DataManager {

	public static String CUSTOM_SALAD_NAME ="@CustomSalad";
	
	/**
	 * This mehtod adds the new item shopping line to the cart. It was separated from
	 * AddtoCart to be able to invoke from the SaladControler.
	 * @param productId
	 */
	public static void addLineCart(ShoppingCart shoppingCart, long productId) {
		//ShoppingCart shoppingCart = getShoppingCart();
		Product product = Product.findById(productId);
		ShoppingCartLine line = new ShoppingCartLine(product);
		shoppingCart.addLine(line);
	}
	
	public static long addToTemporarySalad(String saladIdString, long ingredientId, boolean isAddingIngredient) {
		Salad salad;
		if (saladIdString != null) {
			Long saladId = Long.parseLong(saladIdString);
			salad = Salad.findById(saladId);
		} else {
			// USe empty string to tell that the salad is Temporary
			salad = new VeggieSalad(CUSTOM_SALAD_NAME);
		}
			
		Ingredient ingredient = Ingredient.findById(ingredientId);
		if (isAddingIngredient) {
			if (!salad.ingredients.contains(ingredient))
				salad.ingredients.add(ingredient);
		}
		else
			salad.ingredients.remove(ingredient);
		salad.save();		
		return salad.id;
	}
}
